package com.zryc.shop.sms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.sms.entity.HomeAdvertise;
import com.zryc.shop.sms.service.IHomeAdvertiseService;
import com.zryc.shop.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.Date;

/**
 * <p>
 * 首页轮播广告表 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2023-02-13
 */
@RestController
@RequestMapping("/sms/home/advertise")
@Api(tags = "广告列表管理")
public class HomeAdvertiseController {
    @Autowired
    private IHomeAdvertiseService advertiseService;

    @GetMapping("/list")
    @ApiOperation("广告列表")
    public ResponseResult list(@RequestParam(required = false) @ApiParam(value = "广告名称关键词") String name,
                               @RequestParam(required = false) @ApiParam(value = "广告投放方式：0-PC首页 1-APP首页") Integer type,
                               @RequestParam(required = false) @ApiParam(value = "到期日期") String endTime,
                               @RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                               @RequestParam(defaultValue = "10") @ApiParam(value = "每页显示条数", required = true) Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (type!=null) {
            queryWrapper.eq("type", type);
        }
        if (!StringUtils.isEmpty(endTime)) {
            queryWrapper.le("end_time", endTime);
        }
        advertiseService.page(page, queryWrapper);
        return new ResponseResult(200, "查询完成", page);
    }

    @PostMapping("/create")
    @ApiOperation("新增广告")
    public ResponseResult create(@RequestBody HomeAdvertise advertise) {
        advertise.setClickCount(0);
        advertise.setOrderCount(0);
        advertiseService.save(advertise);
        return new ResponseResult(200, "添加成功");
    }

    @PostMapping("/update/status/{id}")
    @ApiOperation("修改广告状态")
    public ResponseResult updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", status);
        advertiseService.update(updateWrapper);
        return new ResponseResult(200, "修改成功");
    }

    @PostMapping("/delete")
    @ApiOperation("删除广告位")
    public ResponseResult delete(@RequestParam String ids) {
        advertiseService.removeBatchByIds(Arrays.asList(ids.split(",")));
        return new ResponseResult(200, "删除成功");
    }

    @GetMapping("/{id}")
    @ApiOperation("查询广告详情")
    public ResponseResult info(@PathVariable Long id) {
        return new ResponseResult(200, "查询完成", advertiseService.getById(id));
    }

    @PostMapping("/update/{id}")
    @ApiOperation("更新广告内容")
    public ResponseResult update(@PathVariable Long id, @RequestBody HomeAdvertise advertise) {
        advertise.setId(id);
        advertiseService.updateById(advertise);
        return new ResponseResult(200, "修改成功");
    }

}
